<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sys extends MY_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('sys_model','sys');
    }
    

    //判断是否已经登录，消除误操作
	function login(){
	    if(!$this->sys->is_login()){
    		$this->load->view('sys/login');
	    }else{
    	    redirect(site_url('c=product&m=index'));
	    }
	}
	
	//若用户名密码对的上号，就给个session并跳转到主页面。
	function login_do(){   
	    $uname = $this->c_data['uname'];
	    $upass = $this->c_data['upass'];
	    $flag = $this->sys->login($uname,$upass);
	    if($flag){
	        ejd('','',site_url('c=product&m=index'));
	    }else{
	        ejd('','用户名或密码有误！','');
	    }
	    
	}
	
	//退出，即销毁session
	function login_out(){
	    $this->session->sess_destroy();
	    redirect(site_url('c=sys&m=login'));
	}

	function index(){
	    $this->load->view('sys/index',$this->_data);
	}
	
	function menu(){
	    
	    $list = $this->cm->get_all($this->tbl->menu);        //整个menu表对象
	
        $list_menu = there_level_menu($list);	                //排序后的整个menu表对象
            
	    $this->_data['list_menu'] = $list_menu;
	    $this->load->view('sys/menu/index',$this->_data);
	}
	
	function menu_add(){
	    echo $this->load->view('sys/menu/ajax_add',$this->_data,true);
	}
	
	function menu_add_do(){  
	    $this->cm->save($this->tbl->menu,$this->c_data);
	    ejd('','','reload');
	}

	function submenu_add(){
	    echo $this->load->view('sys/menu/sub_ajax_add',$this->_data,true);
	}
		
	function submenu_add_do(){
	    if($this->c_data['depth'] == 2)  ejd('','只能添加到三级导航！');
	    $this->c_data['depth'] += 1;	    
	    $this->cm->save($this->tbl->menu,$this->c_data);
	    ejd('','','reload');
	}

	function menu_del(){
	    $list_menu = $this->cm->get_all($this->tbl->menu,array(),'arr');
	    
	    if(empty($this->c_data['id'])) die;

	    if(have_sub_menu($list_menu, $this->c_data['id'])) ejd('','只能删除叶子类别！');

	   
	    $this->cm->delete($this->tbl->menu,array('id'=>$this->c_data['id']));
	    ejd('','','reload');	     
	}
	
	function menu_edit(){
	    $this->_data['menu'] = $this->cm->get_by_id($this->tbl->menu,$this->c_data['id']);
	    echo $this->load->view('sys/menu/ajax_edit',$this->_data);
	}
	
	function menu_edit_do(){
	    $this->cm->save($this->tbl->menu,$this->c_data);
	    ejd('','','reload');
	}
}
